package com.coolgor.axnut.util;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class PathUtil {
	/**
	 * 获取当前操作系统路径的斜杠符号
	 * 若是win系统，则为“\”
	 * 若是linux系统，则为“/”
	 */	
	private static String seperator = System.getProperty("file.separator");
	
	private static String basePath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
	private static final SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
	private static final Random random = new Random();
	
	/**
	 * 获取项目名称
	 * @return
	 */
	public static String getProjectName() {
        File directory = new File("");// 参数为空
        String courseFile = "";
		try {
			courseFile = directory.getCanonicalPath();
		} catch (IOException e) {
			e.printStackTrace();
		}
        String[] s;
		if (seperator.equals("\\")) {
	        s = courseFile.split("\\\\");
		} else {
	        s = courseFile.split("/");
		}
		String projectName = s[s.length-1];
        return projectName;
	}

	/**
	 * 获取项目资源的根目录
	 * @return
	 */
	public static String getBasePath() {
		//获取系统标识
		String os = System.getProperty("os.name");
		String basePath = "";
		if (os.toLowerCase().startsWith("win")) {
			basePath = "D:/projectspace/" + getProjectName();
		} else {
			basePath = "/Users/airov/work/" + getProjectName();
		}
		//替换上述路径的斜杠
		basePath = basePath.replace("/", seperator);
		return basePath;
	}
	
	/**
	 * 生成随机文件名，当前年月日小时分钟秒钟+五位随机数
	 * 
	 * @return
	 */
	public static String getRandomFileName() {
		// 获取随机的五位数
		int rannum = random.nextInt(89999) + 10000;
		String nowTimeStr = sDateFormat.format(new Date());
		return nowTimeStr + rannum;
	}
	
	/**
	 * 判断目标路径是否存在，如果不存在则新创整个路径
	 * 创建目标路径所涉及到的目录，
	 * 即/home/work/xiangze/xxx.jpg, 那么 home work xiangze
	 * 这三个文件夹都得自动创建
	 * 
	 * @param targetAddr
	 */
	public static void makeDirPath(String targetAddr) {
//		String realFileParentPath = PathUtil.getImgBasePath() + targetAddr;
//		File dirPath = new File(realFileParentPath);
		File dirPath = new File(targetAddr);
		if (!dirPath.exists()) {
			dirPath.mkdirs();
		}
	}
	
	/**
	 * 获取图片的根目录
	 * @return
	 */
	public static String getBarcodePath() {
		//获取系统标识
		String os = System.getProperty("os.name");
		String basePath = "";
		if (os.toLowerCase().startsWith("win")) {
			basePath = "D:/projectspace/" + getProjectName() + "/barcode/";
		} else {
			basePath = "/Users/airov/work/" + getProjectName() + "/barcode/";
		}
		//替换上述路径的斜杠
		basePath = basePath.replace("/", seperator);
		return basePath;
	}
	
	/**
	 * 获取图片的根目录
	 * @return
	 */
	public static String getImgBasePath() {
		//获取系统标识
		String os = System.getProperty("os.name");
		String basePath = "";
		if (os.toLowerCase().startsWith("win")) {
			basePath = "C:/projectdev/image";
		} else {
			basePath = "/Users/airov/work/image";
		}
		//替换上述路径的斜杠
		basePath = basePath.replace("/", seperator);
		return basePath;
	}

	public static String getShopImagePath(long shopId) {
		String imagePath = "/upload/images/item/shop/" + shopId + "/";
		return imagePath.replace("/", seperator);
	}
	
	public static void main(String[] args) {
		getProjectName();
	}
}
